package com.keta.foundation.tag.dwz;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.keta.foundation.util.dwz.Page;

public class PaginationTag extends SimpleTagSupport {

	// 传入分页信息
	private Page page;
	// 设置 每页显示多少
	private int pageNumShown;
	private int begin = 10;
	private int end = 50;
	private int step = 10;
	private String rel;
	private String onchange = "navTabPageBreak({numPerPage:this.value})";
	private String targetType = "navTab";

	@Override
	public void doTag() throws JspException, IOException {

		StringBuilder builder = new StringBuilder();

		builder.append("<div class=\"panelBar\">");
		builder.append("<div class=\"pages\">");
		builder.append("<span>每页显示</span>");
		builder.append("<select name=\"numPerPage\" onchange=\"" + onchange + "\">");

		for (int i = begin; i <= end; i = i + step) {
			if (page.getNumPerPage() == i) {
				builder.append("<option value=\"" + i + "\" selected=\"selected\">" + i + "</option>\n");
			} else {
				builder.append("<option value=\"" + i + "\">" + i + "</option>\n");
			}
		}
		builder.append("</select>");
		builder.append("<span>总条数: " + page.getTotalCount() + "&nbsp;,&nbsp;页数:" + page.getTotalPage() + "</span>");
		builder.append("</div>");

		if (rel != null) {
			builder.append("<div rel=\"" + rel + "\" class=\"pagination\" targetType=\"" + targetType
					+ "\" totalCount=\"" + page.getTotalCount() + "\" numPerPage=\"" + page.getNumPerPage()
					+ "\" pageNumShown=\"" + pageNumShown + "\" currentPage=\"" + page.getPageNum() + "\"></div>\n");
		} else {
			builder.append("<div class=\"pagination\" targetType=\"" + targetType + "\" totalCount=\""
					+ page.getTotalCount() + "\" numPerPage=\"" + page.getNumPerPage() + "\" pageNumShown=\""
					+ pageNumShown + "\" currentPage=\"" + page.getPageNum() + "\"></div>\n");
		}

		builder.append("</div>");

		getJspContext().getOut().write(builder.toString());

		super.doTag();
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}
}
